home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
kermit.columbia.edu
/
kermit.columbia.edu.tar
/
kermit.columbia.edu
/
newsgroups
/
misc.19981211-19990422
/
000383_news@watsun.cc.columbia.edu _Fri Mar 19 12:15:32 1999.msg
< prev
next >
Wrap
Internet Message Format
|
1999-04-21
|
4KB
Return-Path: <news@watsun.cc.columbia.edu>
Received: from newsmaster.cc.columbia.edu (newsmaster.cc.columbia.edu [128.59.59.30])
by watsun.cc.columbia.edu (8.8.5/8.8.5) with ESMTP id MAA22114
for <kermit.misc@watsun.cc.columbia.edu>; Fri, 19 Mar 1999 12:15:31 -0500 (EST)
Received: (from news@localhost)
by newsmaster.cc.columbia.edu (8.8.5/8.8.5) id MAA21965
for kermit.misc@watsun.cc.columbia.edu; Fri, 19 Mar 1999 12:08:50 -0500 (EST)
X-Authentication-Warning: newsmaster.cc.columbia.edu: news set sender to <news> using -f
From: fdc@watsun.cc.columbia.edu (Frank da Cruz)
Subject: Re: Associative Array in Kermit 95
Date: 19 Mar 1999 17:08:49 GMT
Organization: Columbia University
Message-ID: <7cu0b1$lea$1@newsmaster.cc.columbia.edu>
To: kermit.misc@watsun.cc.columbia.edu
In article <7cttsc$47f$1@nnrp1.dejanews.com>, <dn5006@my-dejanews.com> wrote:
: Associative array is a very useful feature in scripting language, it enables
: Perl, Tcl etc. the implementation of comlex data structures. The following
: script demonstrates that associative array can also be crafted in Kermit
: 95. The script counts the unique words in a regular english, french, german,
: etc. text file.
:
: open read testfile.txt
: if fail end 1 Can't open testfile.txt
: assign \%n 0 ; init register
: while true {
: read \%l ; read each line
: if fail break ; until the end of file
: while > \flength(\%l) 0 {
: assign \%w \fbreak(\%l,{ }) ; split on space
: xif defined \m(\%w) { ; word already seen?
: _assign \%w \feval(\m(\%w) + 1) ; incr count this word
: } else {
: _assign \%w 1 ; init count this word
: increment \%n ; next register
: _assign \%n \%w ; register this word
: }
: assign \%l \fltrim(\fright(\%l,-
: \feval(\flength(\%l)-\flength(\%w)))) ; shift to next word
: }
: }
: for \%k 1 \%n 1 {
: assign \%w \m(\%k) ; get word from register
: echo <\m(\%w)> \%w ; display occurences
: }
:
: This approach avoids the use of array which has to be declared in advance.
: The script does not take into account the non alphanumeric characters.
:
: Dat Nguyen
: Airline Telecommunications and Information Services
: 770 Sherbrooke West
: Montreal, Quebec
: Canada H3A 1G1
: Email dat.nguyen&sita.int
:
Excellent! I've had associative arrays on my list for quite a while, but
the list so long and time so short. I've reformatted your script to fit
in 80 columns.
We plan to add a script library to the Kermit website -- this one will
certainly go into it. Other submissions are welcome too; send them in!
(Be sure to document the Kermit program and version and other relevant
info.)
A quick glance shows this script doesn't use any new (post-C-Kermit-6.0)
features, some of which would make it simpler and faster, for example
the new \fword() and \fsplit() functions for extracting words from strings,
with specified break masks (e.g. to make sure punctuation does not count
as part of word (e.g. "thing" and "thing."). Also you can convert each
word to lowercase with \flower() so "Thing", "thing", and "THING" count as
the same word, etc.
Readers should take special note of the "_assign" verb, which is subtly
different from "assign" (see p.457 of the manual).
- Frank